package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118643711";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDLeHRyOzU3UUt6u9S5hX9WnENq4yM2lqDkQzvNaEBPwuA0vwVGKj71hEbGF7podI8L02kqa0fYvKT6+1pFba7LQIc+T0sdJzgM8UN1ctln6LbhSfL5HWZ4AH7LjJ00Hq1bXfCa+k7SR1A0nyHNlHOw9oqShxhuGWK10bfI3cHCZZkKQN9KkAbX4OAh+O+uYDXRPfu0Jb8+OTtWbiarGwN/h8WCM41MYeBTfWOteE+IomVbTPLcWheJ+1KhPrncmz6rJelR7KSI3kKFZc3gCPVsAi9lkbygcvtjD5SAsMLO8IvUH3h64te2wJd/6P7t0I8LEGC1+sdj6dfvDaTzw6BnAgMBAAECggEAQqN4v9DUZQGjDtdlnD00r1EOY3twlPhQSXIbq/Jw3a6db6kAOTKs3AwugcJTstI5502eK95m8X0XK3wW3JZHoRnJMYcWur2VheeeiGDNA1McUjy97wjJ/pNq849iDSfe7wWdP6SrHANvKUzU7VepbFPSnprQTX7S7Wl6H7dBNkEqkDUBDn03Gn6MQ2f235ovIEWmHosgggs8jeTNgEK2iVi9609X+XxmIMeGLTwEpzikCfxTc7DooqllL4+PMfsIuHGhMk1NABm3BPn3vy+6lxMAQ2HJTkg+KSfk3MU3it05cr/bqdHN3qzAHJ4BWN13eHp2qyxAK+THRicb4PbowQKBgQDonZ1WCaqjexfl9eB8NgtD0Z6/yYf4FtHOEJhrGtYFUuZC6U5hZb7lvFy80HR5GFM7qPSZbyqGOUxzhaRDf2bcG1+JgvZL+Ue7osMUiNxyAjog7y5rQFGLoXEUXSNjzAo5xUVDmshKO+QLWDiC9pbzs5JpT8X+tDHqV5ZZZl77lQKBgQDf7Ml/GQhvMotCF6BLr9nf1xvdsPhvHnxW7pvezpLFWojOZ4Co4qwks8e8qCDquT0vlMMN89xQS2k4T1uAvvqgWXSVYpaOmNvPIWhF+CVub6FrzFU8Yh2pj71wVvTYeXHERVGvj1EiUpCcdG0MuYFa9fRKuaj10PWfywJ7czdNCwKBgQDDBGED4NbOeERw5tJtUGHhWbxdzffQZlE/EWq3YKhNjlK9hhvQTBzfPiwBoEIFw/8D5YIroKdmIGOX9lLgw87YeZp/DuyuhS8+gBZNAPDQJE3Xjq67/BBhn9fN2sfwwv9Pr7OCGiIRGmT1neDC6zli1wnIgLjd8PRoI0ZJbSn0gQKBgQDVEQLde91JqUdWmRJ+UhI26gBq3lRVryVStnH7/IUtYirgcx26FQ0FU0dPnI7NqXqtxpges5ZDlK3G1cPUamQhpVzIM+ncP1iP61LDmQ1M2s02vFOyRA3DZLE1rpgFTvFQd0D1kqH5zaQqKqIExJJwEa3kWGqLk8vTwyBWhNfu5wKBgQCvvwpVcXenR8qJy/90hyZn8+bdooM5iCfa4hl8CQa+R5U9hEx/EqN1isO/0PVrZzoyIK6LAUxw+FsrYbHKxEjGjVSp9Blu03N/SIhUFchpL8D1aLHNJXtCkgE3yXZYrjgzLiva3AYNeM59EXo7Xqzi0LzhKLYuivrBXyoI4jbDyw==";    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuLShs2wCPOS7QvxZFJuWzSeh7udS4tVqw3jPYcOG6YB8bKvItSAIpNmwwzi1BCLKLhClZBXXb1pTwZmD7dDz10jkfW1Oswq7N+9sqI474HBoKIK38+SCZOpeWdxX8ywyzEF1oQLTU01Sf2EqUIJ3BZYgZ+YgiUa5dm1WLESo0v1BFW+pDUVFXKTAQUrLsVbnwBRpTELXjFtPLE0hsCIpblGRH1kLIUTMNb0FY4WRSZQX0Hmfg+KAd35LPcPCg6DT/NClOov3UDHWC+d0cVQ29lbNuB5xOIMy03jv3+mbhKJWKinLxQsjOGmGyiuWWKS4LcUADu9ZMchdCEPvYAdIlQIDAQAB";    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://3397594ud8.wicp.vip/payed/ali/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
